﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using UtopianGame.graphics.menu;

namespace UtopianGame
{
    static class GameFlowSystem
    {
        private const int STARTING_BUSINESSES_COUNT = 3;
        private const int RANDOM_EVENT_COUNT = 1;

        private static int currentTurn;

        private static Random rGenerator;

        public static int GetCurrentTurn()
        {
            return currentTurn;
        }

        //Sets up the variables necessary to begin a new game.
        //NOTE: May add parameters to this function later, to 
        //allow for different game modes and options.
        public static void startGame()
        {
            rGenerator = new Random();   //Seeds the game's random number generator.
            //First, generate the game's regions.
            generateRegions();

            //Then, generate random businesses.
            generateStartingBusinesses();

            //Run the initial investment round,
            //where the player can invest in infrastructure
            //for their Nation.
            runStartingInvestmentStage();

            //Generate the policies for the initial pre-round (is this necessary? Might be removed.)
            Nation.GetNewRandomPolicies(rGenerator);

            //Note that after this function runs,
            //the 'Next Turn' button will start a new turn.
        }

        public static void generateRegions()
        {
            Trace.WriteLine("Generate regions.");
        }

        public static void generateStartingBusinesses()
        {
            Trace.WriteLine("Generate starting businesses.");
        }

        public static void runStartingInvestmentStage()
        {
            Trace.WriteLine("Player's starting investment stage.");
        }

        public static void startNewTurn()
        {
            Trace.Write("TURN ");
            Trace.Write(currentTurn);
            Trace.WriteLine(" BEGIN.");

            if (currentTurn <= 50)
            {
                //Generate the turn's random event(s).
                Nation.GetRandomEvents(RANDOM_EVENT_COUNT, rGenerator);

                //Generate random policies for this turn.
                Nation.GetNewRandomPolicies(rGenerator);

                currentTurn++;

                callRandomEvents(RANDOM_EVENT_COUNT);
                businessActionPhase();
                prepPlayerDecisionPhase();
                displayTurnChanges();
                runStatusEffects();
            }
            else
            {
                displayEndingScreen();
            }

        }

        public static void displayEndingScreen()
        {
            String endingNotification = "\n\n\nThe game is finished.\n\nFrom a population of " + Nation.getPopulationCount() + ", you have " + Nation.getPovertyCount() + " citizens still in \npoverty, compared to " + Nation.initialPovertyCount + " at the beginning of the game.";
            if (Nation.getPovertyCount() > Nation.initialPovertyCount)
            {
                endingNotification = endingNotification + "\nDuring your rule, " + (Nation.getPovertyCount() - Nation.initialPovertyCount) + " more people have entered poverty.";
            }
            else
            {
                endingNotification = endingNotification + "\nDuring your rule, " + (Nation.initialPovertyCount - Nation.getPovertyCount()) + " people have risen out of poverty.";
            }
            EndGameScreen endingScreen = new EndGameScreen(GlobalDataHolder.cManager, endingNotification);
            GlobalDataHolder.addGraphicalObject(endingScreen, 20, 20, 10);
            GlobalDataHolder.makeActive(endingScreen);
        }

        public static void callRandomEvents(int randomEventCount)
        {
            Trace.WriteLine("Call random events.");
        }

        public static void businessActionPhase()
        {
            Trace.WriteLine("Businesses take action.");
        }

        public static void prepPlayerDecisionPhase()
        {
            Trace.WriteLine("Prepare for the phase where the player takes control.");
        }

        public static void displayTurnChanges()
        {
            String turnChangeNotification = "\n\n\n" + GlobalDataHolder.randomEventName + "\n \n" + GlobalDataHolder.randomEventDescription;
            MenuScreen turnChangesScreen = new MenuScreen(GlobalDataHolder.cManager, turnChangeNotification);
            GlobalDataHolder.addGraphicalObject(turnChangesScreen, 20, 20, 10);
            GlobalDataHolder.makeActive(turnChangesScreen);
        }

        public static void runStatusEffects()
        {
            for (int i = 0; i < Nation.statusEffectList.Count; i++)
            {
                Trace.WriteLine("Run all status effects.");
                if (Nation.statusEffectList[i] != null)
                {
                    Nation.statusEffectList[i].runBehavior();
                }
            }
        }
    }
}
